package org.jet.emall.rbac.service.tool;

import org.jet.emall.rbac.dto.AdminPrivilegeDTO;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-13 18:08
 */
public abstract class  TreeTool {

    public static List<AdminPrivilegeDTO> makeTree(List<AdminPrivilegeDTO> dtoList){
        Map<Integer,AdminPrivilegeDTO> map = new HashMap<>(128);

        dtoList.forEach(dto->{
            map.put(dto.getId(),dto);
        });
        map.forEach((key,value)->{
            if(value.getSuperId()!=null && value.getSuperId()!=0){
                List<AdminPrivilegeDTO> children = map.get(value.getSuperId()).getChildren();
                if(children==null){
                    children = new LinkedList<>();
                    map.get(value.getSuperId()).setChildren(children);
                }
                children.add(value);
            }
        });
        map.clear();
        dtoList.removeIf(dto-> dto.getSuperId()!=0);
        return dtoList;
    }
}
